﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.Mvc;
using System.Web.Security;
using Winter.BackEnd.Models.DataTypes;

namespace Winter.BackEnd.Models.Domain
{
    public static class Repository
    {
        #region Repository
        [ThreadStatic]
        private static WinterDataContext _DataContext;

        private static aspnet_User currentUser;

        public static WinterDataContext CurrentDataContext
        {
            get
            {
                if (_DataContext == null)
                {
                    _DataContext = CreateNewDataContext();
                }

                return _DataContext;
            }
        }

        public static void UpdateCurrentDataContext()
        {


            _DataContext = CreateNewDataContext();
        }

        public static WinterDataContext CreateNewDataContext()
        {
            WinterDataContext context = new WinterDataContext(ConfigurationManager.ConnectionStrings["WinterConnectionString"].ConnectionString);

            return context;
        }

        public static aspnet_User CurrentUser
        {
            get
            {
                if (Membership.GetUser() != null)
                {
                    return currentUser ?? Repository.CurrentDataContext.aspnet_Users.SingleOrDefault(u => u.UserName.ToLower() == Membership.GetUser().UserName.ToLower()); ;
                }
                //HttpContext.Current.Response.Redirect(FormsAuthentication.LoginUrl);
                return null;
            }
            set
            {
                currentUser = value;
            }
        }

        #endregion

        #region HelperMethods

        public static Group GetGroupByID(int ID)
        {
            return CurrentDataContext.Groups.SingleOrDefault(g => g.ID == ID);

        }

        public static ParameterValue GetParameterValueByID(int ID)
        {
            return Repository.CurrentDataContext.ParameterValues.SingleOrDefault(p => p.ID == ID);
        }

        public static IList<SelectListItem> GetAllGroupForDropDown()
        {
            return (from groups in CurrentDataContext.Groups
                    select
                        new SelectListItem() { Text = groups.Name.ToString(), Value = groups.ID.ToString() }).ToList();
        }

        public static IList<SelectListItem> GetAllDayOfWeekForDropDown()
        {
            return (from days in CurrentDataContext.DayOfWeeks
                    select
                        new SelectListItem() { Text = days.DayOfWeekName.ToString(), Value = days.ID.ToString() }).ToList();
        }

        public static IList<SelectListItem> GetAllSubjectsForDropDown()
        {
            return (from sub in Repository.CurrentDataContext.Subjects
                    select
                        new SelectListItem() { Text = sub.Name.ToString(), Value = sub.ID.ToString() }).ToList();
        }

        public static IList<SelectListItem> GetAllTeachersForDropDown()
        {
            return (from teacher in Repository.CurrentDataContext.Teachers
                    select
                        new SelectListItem() { Text = teacher.Person.LastName.ToString(), Value = teacher.ID.ToString() }).ToList();
        }

        public static IList<Class> GetGroupClassesForSpecificDay(int groupID, System.DayOfWeek dayOfWeek, int count)
        {
            List<Class> classes = new List<Class>();

            for (int i = 0; i < count; i++)
            {
                if (Repository.CurrentDataContext.Classes.Any(c => c.ClassNumber == i && c.WorkDay.Schedule.GroupID == groupID && c.WorkDay.DayOfWeek.ID == (int)dayOfWeek))
                {
                    classes.Add(Repository.CurrentDataContext.Classes.SingleOrDefault(c => c.ClassNumber == i && c.WorkDay.Schedule.GroupID == groupID && c.WorkDay.DayOfWeek.ID == (int)dayOfWeek));
                }
                classes.Add(new Class() { ID = 0, Subject = new Subject() { Name = " " } });
            }
            return classes;
        }
        public static IList<ClassDTO> GetGroupClassesForSpecificDayInJSON(int groupID, System.DayOfWeek dayOfWeek, int count)
        {
            List<ClassDTO> classes = new List<ClassDTO>();

            for (int i = 0; i < count; i++)
            {
                if (Repository.CurrentDataContext.Classes.Any(c => c.ClassNumber == i && c.WorkDay.Schedule.GroupID == groupID && c.WorkDay.DayOfWeek.ID == (int)dayOfWeek))
                {
                    classes.Add(new ClassDTO()
                    {
                        ID = Repository.CurrentDataContext.Classes.SingleOrDefault(c => c.ClassNumber ==
                               i && c.WorkDay.Schedule.GroupID == groupID && c.WorkDay.DayOfWeek.ID == (int)dayOfWeek).ID,
                        Subject =
                           Repository.CurrentDataContext.Classes.SingleOrDefault(c => c.ClassNumber ==
                               i && c.WorkDay.Schedule.GroupID == groupID && c.WorkDay.DayOfWeek.ID == (int)dayOfWeek).Subject.Name
                    });
                }
                classes.Add(new ClassDTO() { ID = 0, Subject = " " });
            }
            return classes;
        }
        #endregion
    }
}
